<?php

namespace lite\controllers;

use lite\models\user\User;
use Yii;
use lite\libs\CommonController;
use lite\models\Weixin;
use yii\base\Exception;

class WeixinController extends CommonController {

    /**
     * @inheritdoc
     */
    public function actions() {

    }

    public function actionQrcode(){
        $scene = yii::$app->request->get("scene");
        $page = yii::$app->request->get("page");
        $weixin=new Weixin();
        $res= $weixin->getQrcode($scene,$page);
        if($res){
            header('Content-type: image/jpg');
            echo $res;
            die();
        }else{
            return $weixin->getErrMsg();
        }
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex() {
        return $this->render("index");
    }

    public function actionLogin(){
      $code = yii::$app->request->post("code");
      $weixin = new Weixin();
      $res = $weixin->login($code);
      if($res) {
          return $this->ajaxResponseSuccess(['access_token'=>$res]);
      } else {
          return $weixin->getErrMsg();
      }
    }

    public function actionUpdate_user_info()
    {
        $userInfo = yii::$app->request->post('user_info');
        $accessToken = yii::$app->request->post('access_token');
        $user = Weixin::checkIsLogin($accessToken);
        if ($user === false) {
            return $this->ajaxResponseError('用户还未登录，请重新登录', -999);
        }
        //保存用户信息
        $nickname = preg_replace_callback('/./u', function(array $match) {
            return strlen($match[0]) >= 4 ? '' : $match[0];
        }, $userInfo['nickName']);
        $user->nickname = $nickname;
        $user->name = $nickname;
        $user->update_time = time();
        $user->sex = $userInfo['gender'];
        $user->avatar = $userInfo['avatarUrl'];
        $result = $user->save();
        if ($result) {
            return $this->ajaxResponseSuccess();
        } else {
            return $this->ajaxResponseError('更新用户信息失败', -1);
        }
    }



    public function actionToken() {
        if($this->checkSignature()) {
            $params = yii::$app->request->getBodyParams();
            Yii::error("微信事件通知:" . print_r($params, true));

            $weixin = new Weixin();
            return $weixin->dealWithEvent($params);
        }
    }


    private function checkSignature() {
        $signature = yii::$app->request->get("signature");
        $timestamp = yii::$app->request->get("timestamp");
        $nonce = yii::$app->request->get("nonce");

        $token = "lvacation";
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);
        if($tmpStr == $signature) {
            return true;
        } else {
            return false;
        }
    }

    /*
    * 判断浏览器是否为微信浏览器
    */

    private function is_weixin() {
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
            return true;
        }
        return false;
    }

}
